﻿/**
 * Created by Administrator on 2017/8/4.
 */
define(function () {
    var CustomerList = {
        name: 'customer-list',
        template: ''
        + '<div v-bind:class="{\'c-two-center\':true,\'c-layout\':divLayout}">'
        + ' <i-table :columns="customercolumns" :data="customerlist" size="small" ref="table" v-on:on-selection-change="selection_change"></i-table>'
        + '</div>',
        props: {
            showType: {
                type: String,
                required: true
            },
            divLayout: Boolean
        },
        data: function () {
            return {
                customerlist: [],
                selectCustomer: [],
            }
        },
        created: function () {
            var thisList = this;
            //通过bus,监听数据绑定事件,监听bindData
            window.bus.$on("bindData", function (data) {
                thisList.customerlist = thisList.setCellClassName(data);
                thisList.selectCustomer = [];
            });
            //通过bus,监听点击转入公客事件,监听clickShiftCustomerToDept
            window.bus.$on("clickShiftCustomerToDept", function (data) {
                if (thisList.checkSelectCustomer()) {
                    //触发showShiftCustomerToDept
                    window.bus.$emit("showShiftCustomerToDept", thisList.selectCustomer);
                }
            });
            //监听clickShiftCustomerToOtherStaff
            window.bus.$on("clickShiftCustomerToOtherStaff", function (data) {
                if (thisList.checkSelectCustomer()) {
                    var staffId = thisList.selectCustomer[0].NodeId;
                    var strCustomerList = [];
                    for (var i = 1; i < thisList.selectCustomer.length; i++) {
                        if (staffId != thisList.selectCustomer[i].NodeId) {
                            thisList.$Message.error("一次只能转移一个销售员的客户");
                            return;
                        }
                        strCustomerList.push(thisList.selectCustomer[i].Id);
                    }
                    strCustomerList.push(thisList.selectCustomer[0].Id);

                    //触发showShiftCustomerToOtherStaff
                    window.bus.$emit("showShiftCustomerToOtherStaff", { customerIdList: strCustomerList, oldStaffId: staffId });
                }
            });
            //监听clickShiftCustomerToOtherDept
            window.bus.$on("clickShiftCustomerToOtherDept", function (data) {
                if (thisList.checkSelectCustomer()) {
                    var nodeId = thisList.selectCustomer[0].NodeId;
                    var strCustomerList = [];
                    for (var i = 1; i < thisList.selectCustomer.length; i++) {
                        if (nodeId != thisList.selectCustomer[i].NodeId) {
                            thisList.$Message.error("一次只能转移一个部门的客户");
                            return;
                        }
                        strCustomerList.push({ customerId: thisList.selectCustomer[i].Id, tradedNodeId: thisList.selectCustomer[i].TradedNodeId });
                    }
                    strCustomerList.push({ customerId: thisList.selectCustomer[0].Id, tradedNodeId: thisList.selectCustomer[0].TradedNodeId });

                    //触发showShiftCustomerToOtherDept
                    window.bus.$emit("showShiftCustomerToOtherDept", { customerList: strCustomerList, oldNodeId: nodeId });
                }
            });
            //监听clickShiftCustomerToStaff
            window.bus.$on("clickShiftCustomerToStaff", function (data) {
                if (thisList.checkSelectCustomer()) {
                    //触发showShiftCustomerToStaff
                    window.bus.$emit("showShiftCustomerToStaff", thisList.selectCustomerId);
                }
            });


            //监听clickAbandonCustomer
            window.bus.$on("clickAbandonCustomer", function (data) {
                if (thisList.checkSelectCustomer()) {
                    //触发showAbandonCustomer
                    window.bus.$emit("showAbandonCustomer", thisList.selectCustomerId);
                }
            });

            //监听clickRecoveredCustomer
            window.bus.$on("clickRecoveredCustomer", function (data) {
                if (thisList.checkSelectCustomer()) {
                    //触发showRecoveredCustomer
                    window.bus.$emit("showRecoveredCustomer", thisList.selectCustomerId);
                }
            });

            //监听bindExportData
            window.bus.$on("bindExportData", function (data) {
                thisList.customerlist = data;
                setTimeout(function () {
                    thisList.$refs.table.exportCsv({
                        filename: '我的客户'
                    });
                }, 0);
                //触发afterExportData
                bus.$emit("afterExportData");
            });
        },
        computed: {
            customercolumns: function () {
                switch (this.showType) {
                    case 'staffCustomer':
                        return [
                            {
                                type: 'selection',
                                width: 40,
                                align: 'left'
                            },
                            {
                                title: '跟踪状态',
                                width: 94,
                                key: 'TrackStatus',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span>{{customerlist[' + params._index + '].TrackStatus}}</span></div>')
                                })
                            },
                            {
                                title: '购买意向',
                                width: 108,
                                key: 'PurchasingIntention',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span style="width:100%">{{customerlist[' + params._index + '].PurchasingIntention}}</span></div></div>')
                                })
                            },
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '销售员',
                                width: 100,
                                key: 'NodeName',
                            },
                            {
                                title: '录入时间',
                                width: 102,
                                key: 'AddTime',
                            },
                            {
                                title: '联系方式',
                                width: 106,
                                key: 'Phone',
                            },
                            {
                                title: '客户地区',
                                width: 110,
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                width: 84,
                                key: 'SourceType',
                            },
                            {
                                title: '意向房源',
                                width: 100,
                                key: 'IntentionHouse',
                            },
                            {
                                title: '最近回访时间',
                                width: 116,
                                key: 'LastVisitTime',
                            },
                            {
                                title: '最近跟踪情况',
                                width: 200,
                                key: 'LastVisitRemark',
                            },
                            {
                                title: '下次回访时间',
                                width: 116,
                                key: 'NextVisitTime',
                            }
                        ];
                    case 'deptOrCompanyCustomer':
                        return [
                            {
                                type: 'selection',
                                width: 40,
                                align: 'left'
                            },
                            {
                                title: '跟踪状态',
                                width: 94,
                                key: 'TrackStatus',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span>{{customerlist[' + params._index + '].TrackStatus}}</span></div>')
                                })
                            },
                            {
                                title: '购买意向',
                                width: 108,
                                key: 'PurchasingIntention',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span style="width:100%">{{customerlist[' + params._index + '].PurchasingIntention}}</span></div></div>')
                                })
                            },
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '客户归属',
                                key: 'NodeName',
                            },
                            {
                                title: '客户地区',
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                key: 'SourceType',
                            },
                            {
                                title: '意向房源',
                                key: 'IntentionHouse',
                            },
                            {
                                title: '最近回访时间',
                                key: 'LastVisitTime',
                            },
                            {
                                title: '最近跟踪情况',
                                key: 'LastVisitRemark',
                            }
                        ];
                    case 'bigCustomer':
                        return [
                            //{
                            //    type: 'selection',
                            //    width: 40,
                            //    align: 'left'
                            //},
                            {
                                title: '跟踪状态',
                                width: 94,
                                key: 'TrackStatus',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span>{{customerlist[' + params._index + '].TrackStatus}}</span></div>')
                                })
                            },
                            {
                                title: '购买意向',
                                width: 108,
                                key: 'PurchasingIntention',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span style="width:100%">{{customerlist[' + params._index + '].PurchasingIntention}}</span></div></div>')
                                })
                            },
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '客户归属',
                                key: 'NodeName',
                            },
                            {
                                title: '客户地区',
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                key: 'SourceType',
                            },
                            {
                                title: '联系方式',
                                key: 'Phone',
                            },
                            {
                                title: '是否成交',
                                key: 'HasTraded',
                            },
                            {
                                title: '客户等级',
                                key: 'LevelName',
                            },
                            {
                                title: '录入时间',
                                key: 'AddTime',
                            },
                            {
                                title: '查看',
                                width: 30,
                                key: 'chakan',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px;width:30px;height:48px"><span></span></div>')
                                })
                            }
                        ];
                    case 'recyclePool':
                        return [
                            {
                                type: 'selection',
                                width: 40,
                                align: 'left'
                            },
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '客户归属',
                                key: 'NodeName',
                            },
                            {
                                title: '客户地区',
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                key: 'SourceType',
                            },
                            {
                                title: '联系方式',
                                key: 'Phone',
                            },
                            {
                                title: '是否成交',
                                key: 'HasTraded',
                            },
                            {
                                title: '录入时间',
                                key: 'AddTime',
                            },
                            {
                                title: '废弃时间',
                                key: 'AbandonTime',
                            },
                            {
                                title: '废弃原因',
                                key: 'AbandonRemark',
                            },
                            {
                                title: '操作',
                                width: 61,
                                key: 'caozuo',
                                render: (function (params) {
                                    return ('<div class="eyes p-click" v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px;width:30px;height:48px;float: left;"><span></span></div>' +
                                        '<div class="recover p-click" v-on:click="cellRecoeredClick(customerlist[' + params._index + '].Id)" style="line-height:48px;width:30px;height:48px;float: right;"><span></span></div>');
                                })
                            }
                        ];

                    case 'tradedCustomer':
                        return [
                            //{
                            //    type: 'selection',
                            //    width: 40,
                            //    align: 'left'
                            //},
                            {
                                title: '跟踪状态',
                                width: 94,
                                key: 'TrackStatus',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span>{{customerlist[' + params._index + '].TrackStatus}}</span></div>')
                                })
                            },
                            {
                                title: '购买意向',
                                width: 108,
                                key: 'PurchasingIntention',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span style="width:100%">{{customerlist[' + params._index + '].PurchasingIntention}}</span></div></div>')
                                })
                            },
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '客户归属',
                                width: 100,
                                key: 'NodeName',
                            },
                            {
                                title: '录入时间',
                                width: 102,
                                key: 'AddTime',
                            },
                            {
                                title: '联系方式',
                                width: 106,
                                key: 'Phone',
                            },
                            {
                                title: '客户地区',
                                width: 110,
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                width: 84,
                                key: 'SourceType',
                            },
                            {
                                title: '意向房源',
                                width: 100,
                                key: 'IntentionHouse',
                            },
                            {
                                title: '最近回访时间',
                                width: 116,
                                key: 'LastVisitTime',
                            },
                            {
                                title: '最近跟踪情况',
                                width: 200,
                                key: 'LastVisitRemark',
                            },
                            {
                                title: '下次回访时间',
                                width: 116,
                                key: 'NextVisitTime',
                            }
                        ];
                    case 'fuzzySearch':
                        return [
                            {
                                title: '姓名',
                                width: 90,
                                key: 'Name',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px"><span>{{customerlist[' + params._index + '].Name}}</span></div>')
                                })
                            },
                            {
                                title: '跟踪状态',
                                width: 94,
                                key: 'TrackStatus',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span>{{customerlist[' + params._index + '].TrackStatus}}</span></div>')
                                })
                            },
                            {
                                title: '购买意向',
                                width: 108,
                                key: 'PurchasingIntention',
                                render: (function (params) {
                                    return ('<div v-on:click="cellClick(customerlist[' + params._index + '].Id)" ><span style="width:100%">{{customerlist[' + params._index + '].PurchasingIntention}}</span></div></div>')
                                })
                            },
                            {
                                title: '客户归属',
                                width: 100,
                                key: 'NodeName',
                            },
                            {
                                title: '录入时间',
                                width: 102,
                                key: 'AddTime',
                            },
                            {
                                title: '联系方式',
                                width: 106,
                                key: 'Phone',
                            },
                            {
                                title: '是否成交',
                                key: 'HasTraded',
                            },
                            {
                                title: '客户地区',
                                width: 110,
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                width: 84,
                                key: 'SourceType',
                            },
                            {
                                title: '意向房源',
                                width: 100,
                                key: 'IntentionHouse',
                            },
                            //{
                            //    title: '操作',
                            //    width: 61,
                            //    key: 'caozuo',
                            //    render: (function (params) {
                            //        return ('<div class="eyes p-click" v-on:click="cellClick(customerlist[' + params._index + '].Id)" style="line-height:48px;width:30px;height:48px;float: left;"><span></span></div>' +
                            //            '<div class="recover p-click" v-on:click="cellRecoeredClick(customerlist[' + params._index + '].Id)" style="line-height:48px;width:30px;height:48px;float: right;"><span></span></div>');
                            //    })
                            //}

                        ];
                    default:
                        return [
                            {
                                type: 'selection',
                                width: 40,
                                align: 'left'
                            },
                            {
                                title: '跟踪状态',
                                key: 'TrackStatus',
                            },
                            {
                                title: '购买意向',
                                key: 'PurchasingIntention'
                            },
                            {
                                title: '姓名',
                                key: 'Name',
                            },
                            {
                                title: '归属部门',
                                key: 'DepartmentName',
                            },
                            {
                                title: '客户地区',
                                key: 'AdministrativeDivisionName',
                            },
                            {
                                title: '客户来源',
                                key: 'CustomerSource',
                            },
                            {
                                title: '意向房源',
                                key: 'IntentionHouse',
                            },
                            {
                                title: '最近回访时间',
                                key: 'LastVisitTime',
                            },
                            {
                                title: '最近跟踪情况',
                                key: 'LastVisitRemark',
                            }
                        ];
                }
            },
            selectCustomerId: function () {
                var strCustomerList = [];
                $.each(this.selectCustomer, function (i, n) {
                    strCustomerList.push(n["Id"]);
                });
                return strCustomerList;
            },
        },
        methods: {
            setCellClassName: function (data) {
                for (var i = 0; i < data.length; i++) {
                    data[i].cellClassName = {};
                    switch (data[i].LastTrackStatus) {
                        case 'WaitVisit':
                            data[i].cellClassName.TrackStatus = 'dengdai';
                            break;
                        case 'ConfirmFlight':
                            data[i].cellClassName.TrackStatus = 'jieji';
                            break;
                        case 'ConfirmLookedHouse':
                            data[i].cellClassName.TrackStatus = 'yidaikan';
                            break;
                        case 'NoVisitPlan':
                            data[i].cellClassName.TrackStatus = 'quedingjieji';
                            break;
                    };
                    switch (data[i].LastPurchasingIntention) {
                        case 'Strong':
                            data[i].cellClassName.PurchasingIntention = 'qianglie';
                            break;
                        case 'Commonly':
                            data[i].cellClassName.PurchasingIntention = 'yiban';
                            break;
                        case 'Weak':
                            data[i].cellClassName.PurchasingIntention = 'jiaoruo';
                            break;
                        case 'NoIntention':
                            data[i].cellClassName.PurchasingIntention = 'wu';
                            break;
                        case 'NoVisit':
                            data[i].cellClassName.PurchasingIntention = 'wei';
                            break;
                    }
                    switch (this.showType) {
                        case 'staffCustomer': break;
                        case 'deptOrCompanyCustomer': break;
                        case 'bigCustomer':
                            if (data[i].HasTraded === "是") {
                                data[i].HasTraded = "√"
                                data[i].cellClassName.HasTraded = "shi"
                            } else {
                                data[i].HasTraded = "×"
                                data[i].cellClassName.HasTraded = "fou"
                            }
                            data[i].cellClassName.chakan = "chakan"
                            break;
                        case 'recyclePool':
                            if (data[i].HasTraded === "是") {
                                data[i].HasTraded = "√"
                                data[i].cellClassName.HasTraded = "shi"
                            } else {
                                data[i].HasTraded = "×"
                                data[i].cellClassName.HasTraded = "fou"
                            }
                            data[i].cellClassName.chakan = "caozuo"
                            break;
                        default: break;
                    }
                }
                return data;
            },
            selection_change: function (selection) {
                this.selectCustomer = selection;
            },
            cellClick: function (id) {
                //触发showCustomerDetail
                window.bus.$emit("showCustomerDetail", { customerId: id });
            },

            cellRecoeredClick: function (id) {
                //触发showRecoveredCustomer
                window.bus.$emit("showRecoveredCustomer", [id]);
            },
            checkSelectCustomer() {
                if (this.selectCustomer.length === 0) {
                    this.$Message.error("没有选择要操作的客户");
                    return false;
                }
                else {
                    return true;
                }
            },

        }
    };
    return CustomerList;
});
